package com.yaoweigroup.community.ui;

import org.apache.http.entity.StringEntity;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.util.JsonUtil;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.UtilHttp;

import android.app.Activity;
import android.os.Bundle;

public class TestActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		 login();
		//reg();
		 //getCode();
	}

	/**
	 * 用户登陆
	 */
	private void login() {
		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();

		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add("UserName", "admin");
		jsonUtil.add("PassWord", "123123");
		LogUtil.d("wxf", jsonUtil.toString());
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString());
		} catch (Exception e) {
		}
		params.setContentType("application/json");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.login, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						LogUtil.d("wxf", "result = " + responseInfo.result);
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());
					}

				});

	}

	/**
	 * 用户注册
	 */

	private void reg() {

		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();

		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add("UserName", "johnwang");
		jsonUtil.add("Password", "wangxinfeng");
		jsonUtil.add("Email", "1016032141@qq.com");
		jsonUtil.add("Address", "安亭镇");
		jsonUtil.add("Mobile", "18201753497");

		LogUtil.d("wxf", jsonUtil.toString());
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString());
		} catch (Exception e) {
		}
		params.setContentType("application/json");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.reg, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						LogUtil.d("wxf", responseInfo.result);
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());
					}

				});

	}

	/**
	 * 获取验证码
	 */

	public void getCode() {

		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();

		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add("phone", "18201753497");
		LogUtil.d("wxf", jsonUtil.toString());
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString());
		} catch (Exception e) {
		}
		params.setContentType("application/json");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.getAuthCode, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						LogUtil.d("wxf", responseInfo.result);

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());

					}

				});
	}

}
